﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：MorePicture.cs
//
//
//文件功能描述：显示商品的所有图片....
//
//
//创建标识：2007.11.19 王志平
//创建描述：
//
//修改2007.1.30 王志平
//修改了分页
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MorePicture : System.Web.UI.Page
{
    //
    //总的页数
    //
    private static int iPageCount;  
    //
    //总的记录数
    //
    private static int iRecords;
    //
    //初始化strProductID
    //
    private string strProductID =string.Empty;
    //
    //初始化XML WEBSERVER 的showProducts的对象
    //
    localhost.ProductService showProducts = new localhost.ProductService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //
            //当绑定第一页时.
            //
            BindDataList(1);
            //
            //获取页的总数
            //
            GetProductCount();
            //
            //绑定页的选择索引
            //
            BindDropdownListPageIndex();
           
        }
    }
    /// <summary>
    /// 异步调用Xml WebServer.
    /// </summary>
    /// <param name="iPageIndex">每一页显示的页面大小</param>
    public void BindDataList(int iPageIndex)
    {
        //
        //设定每一页显示的大小
        //
        int iPageSize = 1;
        //
        //取得strProductID的值
        //
        strProductID = Request.QueryString["productID"].ToString();
        //
        //当strProductID不为空时,异步调用
        //
        if (strProductID != null || strProductID !=string.Empty)
        {
            //
            //Xml WebServer的异步调用.
            //
            showProducts.GetProductImageListByProductIDCompleted += new localhost.GetProductImageListByProductIDCompletedEventHandler(showProducts_GetProductImageListByProductIDCompleted);
            showProducts.GetProductImageListByProductIDAsync(iPageSize, iPageIndex,strProductID);
        }
        //
        //显示当前页
        //
        lblPage.Text = iPageIndex.ToString();

        if (iPageIndex == 1)
        {
            //
            //如果当前页为第一页
            //
            ddlPage.SelectedValue = "0";
        }//if (iPageIndex == 1)
        else
        {
            ddlPage.SelectedValue = (iPageIndex - 1).ToString();
        }//else

        //
        //显示分页按钮的可用性
        //
        ShowButtonPaging();
    }

    void showProducts_GetProductImageListByProductIDCompleted(object sender, localhost.GetProductImageListByProductIDCompletedEventArgs e)
    {
        //
        //DataSet保存结果集
        //
        DataSet dsProductList = new DataSet();

        if (e.Result != null)
        {
            //
            //如果返回结果不空
            //
            dsProductList = e.Result;
            //
            //DataView视图作数据源
            //
            DataView dvProductList = dsProductList.Tables[0].DefaultView;
            //
            //绑定数据源
            //
            dlstProduct.DataSource = dvProductList;
            dlstProduct.DataBind();
        }
    }
    /// <summary>
    /// 获取总的记录条数
    /// </summary>
    protected void GetProductCount()
    {
        //
        //计算出记录总数.
        //
        iRecords = showProducts.GetProductImageCountByProductID(strProductID);

        if (iRecords % 1 == 0)
        {
            //
            //记录数刚好可以整除页大小
            //
            iPageCount = iRecords / 1;
        }
        else
        {
            //
            //记录数不能整除页大小
            //
            iPageCount = Convert.ToInt32(iRecords / 1) + 1;
        }

        lblRecords.Text = iRecords.ToString();
    }

    /// <summary>
    /// 绑定用来选择页导航的下拉列表
    /// </summary>
    protected void BindDropdownListPageIndex()
    {
        ListItem lt;
        for (int i = 0; i < iPageCount; i++)
        {
            lt = new ListItem();
            lt.Text = Convert.ToString(i + 1);
            lt.Value = i.ToString();
            ddlPage.Items.Add(lt);
        }
    }

    /// <summary>
    /// 设定分页按钮的可用性
    /// </summary>
    protected void ShowButtonPaging()
    {
        //
        //初始化时分页按钮的可用
        //
        linkbtnFirst.Enabled = true;
        linkbtnPre.Enabled = true;
        linkbtnNext.Enabled = true;
        linkbtnLast.Enabled = true;

        if (lblPage.Text == "1")
        {
            //如果是第一页，则将首页与上一页的按钮设为不可用
            linkbtnFirst.Enabled = false;
            linkbtnPre.Enabled = false;
            this.linkbtnLast.Enabled = false;
            this.linkbtnNext.Enabled = false;
        }
        else if (int.Parse(lblPage.Text) == iPageCount)
        {
            //如果是最后一页，则将末页与下一页设为不可用
            linkbtnNext.Enabled = false;
            linkbtnLast.Enabled = false;
        }

        else
        {
            //其他情况全部为可用
            linkbtnFirst.Enabled = true;
            linkbtnPre.Enabled = true;
            linkbtnNext.Enabled = true;
            linkbtnLast.Enabled = true;

        }
    }
    /// <summary>
    /// 对DataList数据绑定.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        //
        //从标签中读取图片的ID.
        //
        string strImageID = ((Label)e.Item.FindControl("lblImageID")).Text;
        //
        //将图片显示..
        //
        ((Image)e.Item.FindControl("image")).ImageUrl = "ImagInfo.aspx?productID=" + strProductID + "&&imageID=" + strImageID;
    }
    /// <summary>
    /// 选择页的索引
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlPage_SelectedIndexChanged1(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(ddlPage.SelectedValue) + 1);
        BindDataList(int.Parse(lblPage.Text));
        this.lblPirturePage.Text = lblPirturePage.Text +int.Parse(lblPage.Text);
    }
    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnFirst_Click1(object sender, EventArgs e)
    {
        BindDataList(1);
    }
    /// <summary>
    /// 上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnPre_Click1(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) - 1);
        BindDataList(int.Parse(lblPage.Text));
        this.lblPirturePage.Text = lblPirturePage.Text + int.Parse(lblPage.Text);
    }
    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnNext_Click1(object sender, EventArgs e)
    {
        lblPage.Text = Convert.ToString(Convert.ToInt32(lblPage.Text) + 1);
        this.lblPirturePage.Text = lblPirturePage.Text + int.Parse(lblPage.Text);
        BindDataList(int.Parse(lblPage.Text));
    }
    /// <summary>
    /// 末页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void linkbtnLast_Click1(object sender, EventArgs e)
    {
        BindDataList(iPageCount);
    }
}
